<?php

class cards180count {

	static $excludedCardsValues = array('K','J','Q', '10');


	/**
	 *	Methode qui renvoi les cartes mélangées pour le jeu de 180 points
	 *	
	 */


	public static function get180SuffledCards($path) {

		$new = self::getAllCards($path);
		$new = self::suffleCards($new);
		$new = cards180count::getCards($new);
		$new = cards180count::getCardsWithScores($new);
		return $new;
	}





	public static function getCards($allcards) {
	
		$result = array();
		foreach($allcards as $card) {
		
			$oneOfForbiden = false;
			foreach(self::$excludedCardsValues as $exc) {
				if (strpos($card, $exc) !== false) {
					$oneOfForbiden = true;
				}
			}
			
			if (!$oneOfForbiden) {
				$result[] =  $card;
			}
			
		
		}
		
		return $result;
	}


	public static function getCardsWithScores($allcards) {
	
		$result = array();
		foreach($allcards as $card) {
			$tmp = array();
			$tmp['score'] = self::getScoreFromCard($card) ;
			$tmp['file'] = $card;
			$result[] = $tmp;
		}
		
		return $result;
	}


	public static function getScoreFromCard($filename) {
		
		$arr = explode("-",$filename);
		
		return (int)$arr[0];
	}


	/**
	 *	Methode qui recupere toutes les cartes
	 *	depuis le dossier passé en parametre
	 *	@param	path	chemin vers le dossier contenant toutes les cartes
	 */

	public static function getAllCards($path) {
		$files = scandir($path);

		$new = array();
		foreach($files as $file) {
			if($file != ".." &&  $file != "." &&  $file != ".svn") {
				$new[] = $file;
			}
		}
		
		return $new;
	}
	
	
	/**
	 *	Methode qui melange les cartes d'un array
	 *	@param	arr	array a mélanger
	 */
	 
	public static function suffleCards($arr) {
		shuffle($arr);
		return $arr;
	}


}
